package com.tbit.uqbike.service.business.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.tbit.uqbike.annos.EncapsulateMap;
import com.tbit.uqbike.object.pojo.Recharge;
import com.tbit.uqbike.object.pojo.StatPark;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.service.business.RechargeService;
import com.tbit.uqbike.service.business.SubAccountLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.RechargeDao;
import com.tbit.uqbike.webmanager.dao.core.UserToAgentDao;
import com.tbit.uqbike.webmanager.dao.log.UserRefundLogDao;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 16:01 2020/7/2
 */
@Service
public class RechargeServiceImpl implements RechargeService {

    @Autowired
    private RechargeDao rechargeDao;
    @Autowired
    private UserToAgentDao userToAgentDao;
    @Autowired
    private UserRefundLogDao userRefundLogDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private SubAccountLogService subAccountLogService;

    @Override
    public List<Recharge> queryPage(Integer accountId, String orderNO, Integer rechargeType, Long startTime, Long endTime,
                                    String phone, Integer adAccountId, Integer pageNO, Integer rowCount) {
        Map<String, Object> params = new HashMap<>(10);
        params.put("accountId", accountId);
        params.put("rechargeType", rechargeType);
        params.put("orderNO", orderNO);
        params.put("pageNO", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        params.put("adAccountId", adAccountId);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        params.put("phone", phone);
        Map<Integer, String> areaNameMap = subAccountLogService.getAreaNameMap(accountId);
        List<Recharge> recharges = rechargeDao.queryPage(params);
        for (Recharge recharge : recharges) {
            StatPark statPark = userRefundLogDao.selectByOrderNOAndId(recharge);
            recharge.setRefundMoney(StringUtils.isNull(statPark) ? 0 : statPark.getMoney().doubleValue());
            recharge.setAreaName(areaNameMap.get(recharge.getAccountId()));
        }
        return recharges;
    }

    @Override
    public Integer queryCount(Integer accountId, String orderNO, Integer rechargeType, Long startTime, Long endTime, String phone, Integer adAccountId) {
        Map<String, Object> params = new HashMap<>(10);
        params.put("accountId", accountId);
        params.put("rechargeType", rechargeType);
        params.put("orderNO", orderNO);
        params.put("adAccountId", adAccountId);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        params.put("phone", phone);
        return rechargeDao.queryCount(params);
    }

    @Override
    public List<Recharge> export(Integer accountId, String orderNO, Integer rechargeType, String phone, Integer areaId, Long startTime, Long endTime, Integer adAccountId) {
        Map<String, Object> params = new HashMap<>(9);
        params.put("accountId", accountId);
        params.put("rechargeType", rechargeType);
        params.put("orderNO", orderNO);
        params.put("phone", phone);
        params.put("areaId", areaId);
        params.put("adAccountId", adAccountId);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }

        List<Recharge> recharges = rechargeDao.export(params);
        Map<Integer, String> areaNameMap = subAccountLogService.getAreaNameMap(accountId);
        for (Recharge recharge : recharges) {
            StatPark statPark = userRefundLogDao.selectByOrderNOAndId(recharge);
            recharge.setRefundMoney(StringUtils.isNull(statPark) ? 0 : statPark.getMoney().doubleValue());
            recharge.setAreaName(areaNameMap.get(recharge.getAccountId()));
        }
        return recharges;
    }

    /**
     * @param userId
     * @param time
     * @return java.util.List<com.tbit.uqbike.object.pojo.Recharge>
     * @Description: 测试使用
     * @createDate 2020/12/18 11:44
     */
    @Override
    public List<Recharge> getByTime(Integer userId, String time) {
        /**传递时间过来的前5秒后5秒*/
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("startTime", DateTimeUtil.getBeforeTime(time, 5));
        params.put("endTime", DateTimeUtil.getLastTime(time, 5));
        // DateTimeUtil
        return rechargeDao.getByTime(params);
    }

    @Override
    public JSONObject getRechargeTotal(Integer accountId, Integer rechargeType, Long startTime, Long endTime) {
        Map<String, Object> params = new HashMap<>();
        params.put("accountId", accountId);
        params.put("rechargeType", rechargeType);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        Map<String, Object> sums = rechargeDao.selectRechargeTotal(params);
        JSONObject object = new JSONObject();
        object.put("rechargeMoney", sums.get("rechargeMoney"));
        object.put("rechargeCount", sums.get("rechargeCount"));
        return object;
    }

    @Override
    @EncapsulateMap
    public List<User> getBalanceByArea(HashMap<String, Object> params, Integer accountId, Long startTime, Long endTime, Integer rowCount, Integer pageNO, Boolean isPage) {
        if (isPage) {
            PageHelper.startPage(pageNO, rowCount);
        }
        List<User> users = userToAgentDao.getUserByAccountId(accountId);
        List<Integer> userIds = users.stream().map(User::getUserId).collect(Collectors.toList());
        if (!users.isEmpty()) {
            params.put("userIds", userIds);
            Map<String, Object> maps = new HashMap<>();
            rechargeDao.getBalanceByArea(params).forEach(map -> maps.put(String.valueOf(map.get("userId")), map.get("rechargeMoney")));
            users.forEach(user -> {
                if (user != null) {
                    Integer userId = user.getUserId();
                    user.setRechargeMoney(0);
                    if (maps.containsKey(userId + "")) {
                        user.setRechargeMoney(Integer.parseInt(maps.get(userId + "") + ""));
                    }
                }
            });
        }
        return users;
    }

    @Override
    public Map<String, Object> getBalanceCountByArea(Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("money", userToAgentDao.getUserCountByAccountId(accountId));
        return map;
    }

    @Override
    public Recharge getRechargeByOrderNO(String orderNO) {
        return rechargeDao.selectByOrderNO(orderNO);
    }

}
